package com.yiyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiyi.entity.Result;
import com.yiyi.form.AddResultForm;
import com.yiyi.form.CorrectPaperForm;
import com.yiyi.mapper.ResultMapper;
import com.yiyi.service.ResultService;
import com.yiyi.util.CalculateGradeUtil;
import com.yiyi.util.TypeConverterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yiyi
 * @date 2021/5/31
 */
@Service
public class ResultServiceImpl implements ResultService {

    @Autowired
    private ResultMapper resultMapper;

    /**
     * 保存考试结果基本概况
     *
     * @param addResultForm
     * @return
     * */
    @Override
    public String saveResult(AddResultForm addResultForm) {
        Result result = new Result();
        result.setResultId(String.valueOf(System.currentTimeMillis()));

        result.setExamId(addResultForm.getExamId());
        result.setUserId(addResultForm.getUserId());
        result.setChoiceKey(addResultForm.getChoiceKey().toString());
        result.setAnswerKey(addResultForm.getAnswerKey().toString());

        resultMapper.insert(result);
        return result.getResultId();
    }

    /**
     * 保存选择题结果
     *
     * @param resultId
     * @param realKey
     * @param choiceGrade
     * @return
     * */
    @Override
    public int saveChoiceGrade(String resultId, List<String> realKey, List<Integer> choiceGrade) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("result_id", resultId);
        Result result = resultMapper.selectOne(queryWrapper);
        List<String> resultKey = TypeConverterUtil.stringToStringList(result.getChoiceKey());
        List<Integer> resultChoiceGrade = CalculateGradeUtil.calculateChoice(resultKey, realKey, choiceGrade);
        result.setChoiceGrade(resultChoiceGrade.toString());
        resultMapper.update(result, queryWrapper);
        return 1;
    }

    /**
     * 批改试卷，保存分数
     *
     * @param correctPaperForm
     * @return
     * */
    @Override
    public int saveGrade(CorrectPaperForm correctPaperForm) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("result_id", correctPaperForm.getResultId());
        Result result = resultMapper.selectOne(queryWrapper);

        result.setResultStatus(1);
        result.setAnswerGrade(correctPaperForm.getAnswerGrade().toString());
        List<Integer> choiceGrade = TypeConverterUtil.stringToIntegerList(result.getChoiceGrade());
        int grade = CalculateGradeUtil.calculateGrade(choiceGrade, correctPaperForm.getAnswerGrade());
        result.setResultGrade(grade);
        return resultMapper.update(result, queryWrapper);
    }

    /**
     * 通过id获取结果
     *
     * @param id
     * @return
     * */
    @Override
    public Result findById(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("result_id", id);
        return resultMapper.selectOne(queryWrapper);
    }

    /**
     * 根据用户id和试卷id列表获取结果
     *
     * @param userId
     * @param examIds
     * @return
     * */
    @Override
    public Result findByUerIdAndExamIds(String userId, List<String> examIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        queryWrapper.in("exam_id", examIds);
        return resultMapper.selectOne(queryWrapper);
    }
}
